$(function () {
    // 给提交区域添加点击事件
    $('.submit').click(function () {
        let userVal = $('.user').val();
        let pswVal = $('.psw').val();
        if (!(/^\w+$/.test(userVal))) {
            alert('用户名不符合要求!');
            return;
        }
        if (!(/^[\w\d_]{6,16}$/.test(pswVal))) {
            alert('密码不符合要求!');
            return;
        }
        let xhr = new XMLHttpRequest();
        xhr.open('POST', 'http://39.105.198.203:3001/user/login', true);
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xhr.send(`username=${(userVal)}&password=${(pswVal)}`);
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                let res = JSON.parse(xhr.responseText);
                if (xhr.status === 200) {
                    if (res.code === 200) {
                        alert('登录成功');
                        location.href = 'userPageHaveloginned.html';
                    } else {
                        alert('用户名错误或密码错误');
                    }
                } else {
                    alert('登录请求失败，请稍后再试');
                }
            }
        };
    });
});